﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Diagnostics;
using System.IO;

namespace PowerMonitor.Upgrade
{
    public partial class Upgrader : Form
    {
        public static string strServer = ""; //记录升级服务器
        public static string strUserID = ""; //记录升级频率
        public static string strPwd = ""; //记录升级密码
        //private static string strFrequency = ""; //记录升级频率
        //private static int intHour; //记录升级小时
        //private static int intMin; //记录升级分钟
        //private static string strWeek = ""; //记录升级星期
        //private static string strDate = ""; //记录升级日期
        public static SortedList<string, string> slUpgrade = new SortedList<string, string>();
        public static Hashtable HTable1; //存取本地软件版本及信息
        public static Hashtable HTable2; //存取服务器端软件版本及信息
        private readonly OperateClass operateclass = new OperateClass(); //实例化公共类对象
        private readonly string strName = Application.StartupPath + "\\update\\UpgradeSet.ini"; //定义要读取的INI文件
        private readonly string strNewXmlName = Application.StartupPath + "\\update\\NewVersion\\Update.xml"; //定义要读取的服务器端XML文件
        private readonly string strOldXmlName = Application.StartupPath + "\\update\\Update.xml"; //定义要读取的本地XML文件
        private string strOldVersion = ""; //记录当前版本

        string strVersion = "1.0.0.001";
        string strVersionInfo = "Date:2014.12.29";
        public Upgrader()
        {
            InitializeComponent();
            strServer = operateclass.ReadString("UpgradeSet", "Server", "", strName);
            strUserID = operateclass.ReadString("UpgradeSet", "UserID", "", strName);
            strPwd = operateclass.ReadString("UpgradeSet", "Pwd", "", strName);

            //strFrequency = operateclass.ReadString("UpgradeSet", "Frequency", "", strName);
            //intHour = Convert.ToInt32(operateclass.ReadString("UpgradeSet", "Hour", "", strName));
            //intMin = Convert.ToInt32(operateclass.ReadString("UpgradeSet", "Min", "", strName));
            //strWeek = operateclass.ReadString("UpgradeSet", "Week", "", strName);
            //strDate = operateclass.ReadString("UpgradeSet", "Date", "", strName);
            txtVesion.Text = strVersion;
            txtVesionInfo.Text = strVersionInfo;
            //File.Delete(strOldXmlName); // 删除
            //operateclass.AddXML(strOldXmlName, strVersion, strVersionInfo);
            bLoad = operateclass.Download(Application.StartupPath + "\\update\\NewVersion\\", "Update.xml", strServer, strUserID, strPwd, "TestMonitor/");
        }
        bool bLoad = false;
        private void Upgrade_Load(object sender, EventArgs e)
        {
            ControlUpdate(false);
        }
        private void btnUpgrade_Click(object sender, EventArgs e)
        {
            ControlUpdate(true);
        }

        public bool CheckVersion()
        {
            if (bLoad == false)
            {
                return false;
            }
           return ControlUpdate(false);
        }
        /// <summary>
        ///   控制版本升级
        /// </summary>
        private bool ControlUpdate(bool bDirectUpgrade)
        {
            bool bRes = false; 
            try
            {
                HTable1 = operateclass.SelectXML(strOldXmlName);
                HTable2 = operateclass.SelectXML(strNewXmlName);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }

            
            IDictionaryEnumerator IDEnumerator1 = HTable1.GetEnumerator();
            IDictionaryEnumerator IDEnumerator2 = HTable2.GetEnumerator();
            string str = "";
            while (IDEnumerator1.MoveNext())
            {
                if (str == "")
                    str = IDEnumerator1.Key.ToString();
                else
                    str += "\n" + IDEnumerator1.Key;
            }
            //获取本地机器的最新版本
            if (str.IndexOf("\n") != -1)
                strOldVersion = str.Substring(str.LastIndexOf("\n") + 1, str.Length - str.LastIndexOf("\n") - 1);
            else
                strOldVersion = str;
            //使用本地机器上的最新版本与服务器上的新版本进行比较
            while (IDEnumerator2.MoveNext())
            {
                if (string.Compare(strOldVersion, IDEnumerator2.Key.ToString()) < 0)
                {
                    if (string.IsNullOrEmpty(txtLastVersion.Text))
                    {
                        txtLastVersion.Text = IDEnumerator2.Key.ToString();
                    }
                    slUpgrade.Add(IDEnumerator2.Key.ToString(), IDEnumerator2.Value.ToString());
                }
            }
            if (string.IsNullOrEmpty(txtLastVersion.Text)) // 当前已是最新版本
            {
                txtLastVersion.Text = strVersion;
            }

            if (slUpgrade.Count > 0)
            {
                if (bDirectUpgrade || DialogResult.Yes == MessageBox.Show("软件有新版本，是否要升级?", Properties.Resources.msgTitleTip, MessageBoxButtons.YesNo, MessageBoxIcon.Information))
                {
                    frmUpdate frmupdate = new frmUpdate();
                    if (frmupdate.ShowDialog() == DialogResult.OK)
                    {
                        bRes = true;
                        goto Finish;
                    }
                    else
                    {
                        bRes = false;
                        goto Finish;
                    }
                }
                else
                {
                    bRes = false;
                    goto Finish;
                }
            }
            else
            {
                if (bDirectUpgrade)
                {
                    MessageBox.Show("当前版本已经是最新版本，不需要升级！", Properties.Resources.msgTitleTip, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                File.Copy(strNewXmlName, strOldXmlName, true);
                bRes = false;
                goto Finish;
            }
        Finish:
            if (bRes)
            {
                File.Copy(strNewXmlName, strOldXmlName, true);
            }
            return bRes;
               
        }



    }
}
